# encoding: utf-8
# @author: wangxin
# @software: garner
# @file: centerViews.py
# @time: 2025/2/20 16:27

from http.client import responses
from lib2to3.fixes.fix_input import context

from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import View
from urllib3 import request


class centerView(View):
    def get(self, request):
        if request.user.is_authenticated:

            user = request.user
            context={
                'username':user.username,
                'mobile':user.mobile,
                # 用户头像URL，如果用户设置了头像，则返回头像的URL；否则返回None
                'avatar': user.avatar.url if user.avatar else None,
                'user_desc':user.user_desc
            }

            return render(request, 'center.html',context=context)
        else:
            return render(request, 'login.html')
    def post(self,request):
        user = request.user
        avatar = request.FILES.get('avatar')

        username=request.POST.get('username')
        user_desc=request.POST.get('desc')
        try:
            user.username=username
            user.user_desc=user_desc
            if avatar:
                user.avatar=avatar
            user.save()
        except Exception as e:
            print(e)
            return render(request,'center.html',{'error':'修改失败'})
        responses=redirect(reverse('users:center'))
        #更新cookie
        responses.set_cookie('username',user.username)
        return responses



